/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.boundary.user;

import carrello.impl.controller.registereduser.ShoppingCartCTRL;
import java.util.HashMap;
import carrello.impl.boundary.AbstractUI;
import carrello.model.entities.Product;
import carrello.impl.controller.user.*;
import carrello.model.entities.Category;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author Mehdi
 */
public class ProductUI extends AbstractUI {
    Logger log = Logger.getLogger(ProductUI.class);
    private int productid;
    private Product product = null;
    private List productExtraInfo = null;
    private Category cat;
    private int numOfExtraInfo = 0;
    private String [] attrName;
    private String [] attrVal;
    private int save = 1;
    private LinkedList<Product> relatedProducts;
    
    public LinkedList<Product> getrelatedProducts () {
        return relatedProducts;
    }
    
    public void setrelatedProducts ( LinkedList<Product> prod ) {
        relatedProducts = prod;
    }
    
    public Category getCat() {
        return cat;
    }

    public void setCat(Category cat) {
        this.cat = cat;
    }
    public Product getProduct () {
        return product;
    }

    /**
     * @return the Productid
     */
    public int getProductid() {
        return productid;
    }

    /**
     * @param productid the productid to set
     */
    public void setProductid(int productid) {
        this.productid = productid;
    }


    public int getSave() {
        return save;
    }

    public void setSave(int save) {
        this.save = save;
    }
    
    /**
     * @return the attrName
     */
    public String[] getAttrName() {
        return attrName;
    }

    /**
     * @param attrName the attrName to set
     */
    public void setAttrName(String[] attrName) {
        this.attrName = attrName;
    }

    /**
     * @return the attrVal
     */
    public String[] getAttrVal() {
        return attrVal;
    }

    /**
     * @param attrVal the attrVal to set
     */
    public void setAttrVal(String[] attrVal) {
        this.attrVal = attrVal;
    }

    /**
     * @return the productExtraInfo
     */
    public List getProductExtraInfo() {
        return productExtraInfo;
    }

    /**
     * @param productExtraInfo the productExtraInfo to set
     */
    public void setProductExtraInfo(List productExtraInfo) {
        this.productExtraInfo = productExtraInfo;
    }

    /**
     * @return the numOfExtraInfo
     */
    public int getNumOfExtraInfo() {
        return numOfExtraInfo;
    }

    /**
     * @param numOfExtraInfo the numOfExtraInfo to set
     */
    public void setNumOfExtraInfo(int numOfExtraInfo) {
        this.numOfExtraInfo = numOfExtraInfo;
    }
    
    private void fillRelatedProducts ( ) {
        relatedProducts = ProductCTRL.getRelatedProducts (product);
    }
    @Override
    public String execute() 
    {
        try {
        List<Category> catlist = Category.getAllCategories();
        for (Category ct : catlist) {
            setCat(ct);
        }
        product = ProductCTRL.getProductById(productid);
        fillRelatedProducts ();
        productExtraInfo = ProductCTRL.getProductInfoById(getProductid());
        if (!productExtraInfo.isEmpty()){
            setProductExtraInfo(productExtraInfo);
            setNumOfExtraInfo(productExtraInfo.size());
        }
        }catch (Exception e) {
            log.error(e);
        }
        if ( save == 0) {
            addToCart();
            save = 1;
            return SUCCESS;
        }
        

        return SUCCESS;
    }
    
    
    public void addToCart() 
    {
        // creating the product info map
        Map<Integer, Integer> cartMap = null; 
        int quantity = 0;
        ShoppingCartCTRL st = new ShoppingCartCTRL();
        // save to cart
        
        if (getSession().containsKey(getSessionUsername())) {
            cartMap = (Map<Integer, Integer>)getSession().get(getSessionUsername());
            if (cartMap.containsKey(productid)) {
                 quantity = st.updateCart(cartMap.get(productid));
            } else {
                quantity = 1;
            }
            
            cartMap.put(productid, quantity);
            setSessionCart(cartMap);
        } else {
            try {
                cartMap = new HashMap<Integer, Integer>();
                cartMap = st.saveToCart(productid);
            } catch (Exception e) {
                log.info(e);
                e.getMessage();
            } // try
            setSessionCart(cartMap);  
        } // if
    } // addToCart
}
